package com.youzhi.backend.aspect;

import com.youzhi.backend.exception.EcpEnum;
import com.youzhi.backend.exception.YouZhiException;
import com.youzhi.backend.request.PageRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author xumeng
 * @Description
 */
@Aspect
@Component
public class PageRequestAspect {


    @Pointcut("execution(public * com.youzhi.backend.controller.*Controller.*(..))")
    public void method() {
    }


    /**
     * 切入点
     *
     * @param joinPoint
     */
    @Before("method()")
    public void doBefore(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof PageRequest) {
                PageRequest pageRequest = (PageRequest) arg;
                if (pageRequest.getCurrent() == null) {
                    throw YouZhiException.newException(EcpEnum.field_no_blank, PageRequest.Fields.current);
                }
                if (pageRequest.getSize() == null) {
                    throw YouZhiException.newException(EcpEnum.field_no_blank, PageRequest.Fields.size);
                }
            }
        }
    }


}
